<?php
namespace app\backstage\logic;
use think\Db;
class Console
{
    /*近30天充值及今日充值总额*/
    public function getTotalRecharge($type)
    {
        $table = 'rp_income_summary_daily_channel';
        $uid = request()->_user->id;

        if (request()->_role->alias == 'agent') {
            $table = 'rp_income_summary_daily_channel';
            $Account = \think\facade\App::model('Account', 'logic');
            $channels = $Account->getChannels(request()->_user->id);
            $channel_id_gh = $channels ? array_column($channels,'id') : [999999999];
            $where[] = ['channel_id', 'in', $channel_id_gh];
        }
        if (request()->_role->alias == 'channel') {
            $table = 'rp_income_summary_daily_channel';
            $where[] = ['channel_id','=',$uid];
        }

        if (request()->_role->alias == 'eb') {
            $table = 'rp_income_summary_daily_agent';
            $Account = \think\facade\App::model('Account', 'logic');
            $agents = $Account->getAgents(request()->_user->id);
            $agent_id_gh = $agents ? array_column($agents,'id') : [999999999];

            $where[] = ['agent_id','in',$agent_id_gh];
        }


        if($type == 1)
        {
            //今日充值汇总
            $where[] = ['create_time','>=',date('Y-m-d 00:00:00')];
            $where[] = ['status','=',2];
            $table = 'pay_order_success';
            $field = 'total_fee';
        }else{
            //近30天充值
            $start_date = date('Y-m-d', strtotime('-29days'));
            $end_state = date('Y-m-d');
            $where[] = ['report_date','>=',$start_date];
            $where[] = ['report_date','<=',$end_state];
            $field = 'recharge_amount';
        }

        $money = Db::table($table)->where($where)->sum($field);

        return getQuotient($money,100);
    }

    /*累计扣量总额及今日扣量总额*/
    public function getTotalDeductionAmount($type)
    {
        if($type == 1)
        {
            //今日充值汇总
            $where[] = ['create_date','=',date('Y-m-d')];
        }else{
            //近30天充值
            $start_date = date('Y-m-d', strtotime('-29days'));
            $end_state = date('Y-m-d');
            $where[] = ['create_date','>=',$start_date];
            $where[] = ['create_date','<=',$end_state];
        }

        return 0;
    }

    /*近30天结算及待结算金额*/
    public function getTotalSettlementAmount($type)
    {
        if($type == 1)
        {
            //待结算金额
            if (request()->_role->alias == 'eb') {
                $Account = \think\facade\App::model('Account', 'logic');
                $agents = $Account->getAgents(request()->_user->id);
                $agent_id_gh = $agents ? array_column($agents,'id') : [999999999];

                $where_income[] = ['agent_id','in',$agent_id_gh];
                $where_set[] = ['agent_id','in',$agent_id_gh];

            }
            if (request()->_role->alias == 'agent') {
                $where_income[] = ['agent_id','=',request()->_user->id];
                $where_set[] = ['agent_id','=',request()->_user->id];
            }

            $start_date = date('Y-m-d', strtotime('-29days'));
            $end_state = date('Y-m-d');

            $where_income[] = ['report_date','>=',$start_date];
            $where_income[] = ['report_date','<=',$end_state];

            $where_set[] = ['create_date','>=',$start_date];
            $where_set[] = ['create_date','<=',$end_state];
            $where_set[] = ['status','>',1];

            $recharge_amount = Db::table('rp_income_summary_daily_agent')->where($where_income)->sum('recharge_amount');
            $SettlementAmount = Db::table('cms_withdrawal_record')->where($where_set)->sum('money');
            
            $money = $recharge_amount - $SettlementAmount;

        }else{
            //近30天已结算金额包含已审未打款
            $start_date = date('Y-m-d', strtotime('-29days'));
            $end_state = date('Y-m-d');

            if (request()->_role->alias == 'eb') {
                $Account = \think\facade\App::model('Account', 'logic');
                $agents = $Account->getAgents(request()->_user->id);
                $agent_id_gh = $agents ? array_column($agents,'id') : [999999999];

                $where[] = ['agent_id','in',$agent_id_gh];
            }

            if (request()->_role->alias == 'agent') {
                $where[] = ['agent_id','=',request()->_user->id];
            }

            $where[] = ['create_date','>=',$start_date];
            $where[] = ['create_date','<=',$end_state];
            $where[] = ['status','>',1];
            $money = Db::table('cms_withdrawal_record')->where($where)->sum('money');
        }

        return getQuotient($money,100);
    }

    /*代理结算金额及代理待结算金额*/
    public function getTotalSettlementAmountAgent()
    {
        //已结算金额
        $where_set[] = ['status','>',1];
        $where_income = [];

        if (request()->_role->alias == 'eb') {

            $Account = \think\facade\App::model('Account', 'logic');
            $agents = $Account->getAgents(request()->_user->id);
            $agent_id_gh = $agents ? array_column($agents,'id') : [999999999];

            $where_set[] = ['agent_id','in',$agent_id_gh];
            $where_income[] = ['agent_id','in',$agent_id_gh];

        }

        //充值总额
        $recharge_amount = Db::table('rp_income_summary_daily_agent')->where($where_income)->sum('recharge_amount');
        $settlement_amount = Db::table('cms_withdrawal_record')->where($where_set)->sum('money');

        $data['settlement_amount'] = getQuotient($settlement_amount,100);
        $data['unsettlement_amount'] = getQuotient(($recharge_amount-$settlement_amount),100);

        return $data;
    }

    /*累计用户数*/
    public function getTotalUserNum($type)
    {
        $table = 'rp_recycling_report_daily_agent';
        $uid = request()->_user->id;
        $where = [];

        if (request()->_role->alias == 'agent') {
            $table = 'rp_recycling_report_daily_agent';
            $where[] = ['agent_id','=',$uid];
        }
        if (request()->_role->alias == 'channel') {
            $table = 'rp_recycling_report_daily_channel';
            $where[] = ['channel_id','=',$uid];
        }

        if (request()->_role->alias == 'eb') {
            $table = 'rp_recycling_report_daily_agent';
            $Account = \think\facade\App::model('Account', 'logic');
            $agents = $Account->getAgents(request()->_user->id);
            $agent_id_gh = $agents ? array_column($agents,'id') : [999999999];

            $where[] = ['agent_id','in',$agent_id_gh];

        }

        if($type == 1)
        {
            $num = Db::table($table)->where($where)->sum('new_fan_num');
        }else{
            $num = Db::table($table)->where($where)->sum('jiazhuo_num');
        }

        return $num;
    }

}